// pages/clockin/clockin.js
import {request, requestD} from "../../request/request.js"
import {getSetting,chooseAddress,openSetting,getLocation} from "../../utils/asyncWx.js"
const util = require('../../utils/util.js')

const ak = 'T1ee49G9uSKzenafoxkHAbfGGEB3qQV2'

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    location:{
      formAddr:'',
      business:''
    },
    clockinLog:{
      time:''
    }
  },

  //获取地址
  async handleGetLocation(e){
    try {
      // 获取权限状态
      const res1 = await getSetting();
      const scopeUserLocation = res1.authSetting["scope.userLocation"];
      // 判断权限状态
      if(scopeUserLocation===false){
        await openSetting();
      }
      // 调用获取用户地址api获取坐标
      let coord = await getLocation();
      // 地址逆解析
      let params={
        url:'',
        data:{
          ak:ak,
          output:'json',
          coordtype:'wgs84ll',
          location: coord.latitude+','+coord.longitude
        }
      }
      request(params, 'https://api.map.baidu.com/reverse_geocoding/v3/')
      .then(result=>{
        let {formatted_address,business} = result.data.result
        let location = {
          formAddr:formatted_address,
          business:business
        }
        this.setData({
          location
        })
      })

    } catch (error) {
      console.log(error)
      if(error.errCode===2){
        wx.showToast({
          title: '请打开GPS',
          icon: 'none',
          duration: 1000,
          mask: true
        });
      } 
    }
  },

  //更新时间
  updateTime(hm){
    this.setData({
      clockinLog:{
        time:hm
      }
    })
  },

  //打卡
  handleClockin(e){
    const location = this.data.location
    if(location.formAddr===''){
      wx.showToast({
        title: '请获取位置',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return
    }
    let newdate = new Date()
    let day = newdate.getDate()
    let time = util.formatTimeHM(newdate)

    wx.setStorageSync("clockinLog", {day:day, time:time, location:location})
    this.updateTime(time)

    const w_pid = wx.getStorageSync("user").pid;
    requestD({
      url:'clockin/addClockin',
      data:{
        w_pid,
        formAddr:location.formAddr,
        business:location.business,
        remark:''
      }
    }).then(result=>{
      const resp = result.data
      if(resp.error_code!=0){
        console.log(resp)
        wx.showToast({
          title: '未上传',
          icon: 'none',
          duration: 800
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      mask: true,
    })
    let clockinLog = wx.getStorageSync("clockinLog");
    if(clockinLog.day!=null){
      let prev = clockinLog.day
      let curr = new Date().getDate()
      if(prev==curr){
        this.updateTime(clockinLog.time)
        this.setData({location:clockinLog.location})
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})